<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        .slider {
            position: relative;
            width: 800px;
            height: 327px;
            margin: 20px auto;
        }

        .slider img {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            transition: all 1s;
        }

        .slider-item:nth-of-type(1) {
            z-index: 1;
        }

        .arrow {
            position: absolute;
            width: 40px;
            height: 80px;
            line-height: 80px;
            background-color: rgba(0, 0, 0, 0.1);
            color: #fff;
            top: 50%;
            transform: translateY(-50%);
            font-size: 40px;
            text-align: center;
            font-family: 黑体;
            cursor: pointer;
            /* display: none; */
            opacity: 0;
            transition: all 1s;
            z-index: 999;
        }

        .slider:hover .arrow {
            /* display: block; */
            opacity: 1;
        }

        .arrow-left {
            left: 0;
        }

        .arrow-right {
            right: 0;
        }
    </style>
</head>

<body>
    <div class="slider">
        <img class="slider-item" src="./imgs/01.jpg" alt="">
        <img class="slider-item" src="./imgs/02.jpg" alt="">
        <img class="slider-item" src="./imgs/03.jpg" alt="">
        <img class="slider-item" src="./imgs/04.jpg" alt="">
        <img class="slider-item" src="./imgs/05.jpg" alt="">
        <img class="slider-item" src="./imgs/06.jpg" alt="">
        <div class="arrow arrow-left"> &lt; </div>
        <div class="arrow arrow-right"> &gt; </div>
    </div>
    <script>
        // 获取相关元素的dom对象
        var imgs = document.querySelectorAll('.slider img')
        var arrow = document.querySelectorAll('.arrow')
        // 给右箭头注册点击事件
        // 用来记录当前显示的图片序号
        var current = 0
        arrow[1].onclick = function () {
            current++
            if (current > imgs.length - 1) {
                current = 0
            }
            // 切换图片
            play()
        }
        // 左箭头
        arrow[0].onclick = function () {
            current--
            if (current < 0) {
                current = imgs.length - 1
            }
            // 切换图片
            play()
        }

        function play() {
            // 1- 将所有图片的透明度设置为0
            imgs.forEach(function (img) {
                img.style.opacity = 0
            })
            // 2- 将当前序号对应的图片透明度设置为1
            imgs[current].style.opacity = 1
        }
    </script>
</body>

</html>